package auto.test.base;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import auto.test.util.GetConfigPath;
import auto.test.util.ProUtil;
import auto.test.util.WaitUtil;

//动作基类
public class BaseHandle extends WaitUtil{
	public WebDriver driver;
	GetConfigPath getconfigpath=new GetConfigPath();
	ProUtil pro=new ProUtil(getconfigpath.GetconfigPath());
	static Logger logger=Logger.getLogger(BaseHandle.class);

	public BaseHandle(WebDriver driver) {
		this.driver = driver;
	}
	
	public String GetTitle() {
		return driver.getTitle();
	}
	
	//主场无法直接注入cookie获取登录状态

	public void SetCookie() {
		
		 String value="7afc0583bc79413799e25dbd8ca14b0d";
		 Cookie cookie=new Cookie("JSESSIONID",value,".zhan.zzxes.com.cn","/",null);
		 driver.manage().addCookie(cookie);
		 System.out.print(cookie);
}
	
	
	public void Clear(WebElement element) {
		logger.info("清除了【"+element+"】 的内容");

		element.clear();
		
	}
	
	public void Sendkey(WebElement element,String key) {
		logger.info("在文本框【"+element+"】 中输入内容");

		element.sendKeys(key);;
		
	}
	
	public void Click(WebElement element) {
		logger.info("点击了元素【"+element+"】 ");
		element.click();
		sleep(2000);
		
	}
	
	public String GetText(WebElement element) {
		logger.info("获取了【"+element+"】 的文本");

		return element.getText();
	
	}


	public void MoveToElement(WebElement ToElement){
		logger.info("鼠标移动至【"+ToElement+"】");
		Actions MoseAction=new Actions(driver);
		MoseAction.moveToElement(ToElement).perform();
	}
	


	public void  SlippingToElement(WebElement ToElement){
		logger.info("滚动条滑动至【"+ToElement+"】");
		((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", ToElement);
	}

	public void  SlippingTobottom(){
		logger.info("滚动条滑动到底");
		((JavascriptExecutor) driver).executeScript("document.documentElement.scrollTop=100000");
	}



	//通过资源路径文件配置去操作点击元素并完成上传操作
	public void upload(WebElement element) throws Exception {
		    element.click();
	
		    Robot robot=new Robot();

		    Thread.sleep(1000);
		    ProUtil pro =new ProUtil(getconfigpath.GetresourcePath());
		    StringSelection selection=new StringSelection(pro.GetPro("imgpath"));

		    //把图片路径复制到剪切板

		    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);

		    //按下Ctrl+V

		    robot.keyPress(KeyEvent.VK_CONTROL);

		    robot.keyPress(KeyEvent.VK_V);

		    //释放Ctrl+V

		    robot.keyRelease(KeyEvent.VK_CONTROL);

		    robot.keyRelease(KeyEvent.VK_V);

		    Thread.sleep(2000);

		    //点击回车

		    robot.keyPress(KeyEvent.VK_ENTER);

		    robot.keyRelease(KeyEvent.VK_ENTER);

		
	}
//aotuit 脚本上传实现
	public void upload1(WebElement element) throws Exception {
		element.click();
		Thread.sleep(1000);
		Runtime.getRuntime().exec("D:/upload.exe");

	}


/*	public static String getDownloadedDocumentName(String filepath, String filename)
    {	
    	String downloadedFileName = null;
    	boolean valid = true;
    	boolean found = false;

    	//default timeout in seconds
    	long timeOut = 30; 
    	try 
    	{					
    		Path downloadFolderPath = Paths.get(filepath);
    		WatchService watchService = FileSystems.getDefault().newWatchService();
    		downloadFolderPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
    		long startTime = System.currentTimeMillis();
    		do 
    		{
    			WatchKey watchKey;
    			watchKey = watchService.poll(timeOut,TimeUnit.SECONDS);
    			long currentTime = (System.currentTimeMillis()-startTime)/1000;
    			if(currentTime>timeOut)
    			{
    				System.out.println("Download operation timed out.. Expected file was not downloaded");
    				return downloadedFileName;
    			}
    			
    			for (WatchEvent<?> event: watchKey.pollEvents())
    			{
    				WatchEvent.Kind<?> kind = event.kind(); 
    				if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) 
    				{
    					String fileName = event.context().toString();
    					System.out.println("New File Created:" + fileName);
    					if(fileName.endsWith(filename))
    					{
    						downloadedFileName = fileName;
    						System.out.println("Downloaded file found with extension " + filename + ". File name is " + 

    fileName);
    						Thread.sleep(500);
    						found = true;
    						break;
    					}
    				}
    			}
    			if(found)
    			{
    				return downloadedFileName;
    			}
    			else
    			{
    				currentTime = (System.currentTimeMillis()-startTime)/1000;
    				if(currentTime>timeOut)
    				{
    					System.out.println("Failed to download expected file");
    					return downloadedFileName;
    				}
    				valid = watchKey.reset();
    			}
    		} while (valid);
    	} 
    	
    	catch (InterruptedException e) 
    	{
    		System.out.println("下载中断 - " + e.getMessage());
    		e.printStackTrace();
    	}
    	catch (NullPointerException e) 
    	{
    		System.out.println("下载超时，没有找到所需文件");
    	}
    	catch (Exception e)
    	{
    		System.out.println("Error occured - " + e.getMessage());
    		e.printStackTrace();
    	}
    	return downloadedFileName;
}
*/

	
}
